package com.loong.data.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.loong.common.rabbitmq.dto.OrderDTO;
import com.loong.common.utils.TransformUtils;
import com.loong.data.common.backend.result.GoodsResult;
import com.loong.data.model.OrderDeliveryModel;
import com.loong.data.model.OrderItemsModel;
import com.loong.data.model.OrderModel;
import com.loong.data.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Description: es 订单服务
 * @Author: hzp
 * @Date: 2021-1-2 22:07
 */
@Component
@Slf4j
public class OrderHandler {
    @Resource
    private ElasticsearchRestTemplate template;

    @Resource
    private RestHighLevelClient client;

    @Resource
    private OrderRepository orderRepository;


    /**
     * 提交订单信息到es：MQ消费者中调用该方法
     *
     * @param orderDTOList 订单参数
     */
    public void saveOrderModel(List<OrderDTO> orderDTOList) {
        List<OrderModel> OrderModelList = parseDataList(orderDTOList);
        orderRepository.saveAll(OrderModelList);
    }

    /**
     * 修改es中订单信息 MQ消费者中调用该方法
     * @param orderDTOList 订单参数
     */
    public void updateOrderModel(List<OrderDTO> orderDTOList){
        List<OrderModel> OrderModelList = parseDataList(orderDTOList);
        OrderModelList.forEach(orderModel -> {
            //创建更新请求
            UpdateRequest request = new UpdateRequest("order", orderModel.getId().toString());
            //将要更新的数据以json格式封装到请求中
            request.doc(JSON.toJSONString(orderModel), XContentType.JSON);
            //执行更新请求的操作
            try {
                client.update(request, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
    /**
     * 根据id删除es中的订单信息：MQ消费者中调用该方法
     *
     * @param orderDTOList 订单集合
     */
    public void deleteShopModel(List<OrderDTO> orderDTOList) {
        List<OrderModel> OrderModelList = parseDataList(orderDTOList);
        orderRepository.deleteAll(OrderModelList);

    }

    /**
     * 根据id删除订单
     * @param id 订单id
     */
    public void deleteById(Long id){
        orderRepository.deleteById(id);
    }

    /**
     * 根据id 查找ES order
     * @param id id
     * @return OrderModel
     */
    public OrderModel findOrderModelById(Long id){
        Optional<OrderModel> optional = orderRepository.findById(id);
        return optional.orElse(null);
    }


    /**
     * 构建esModel
     * @param orderDTOList 订单集合
     * @return List<OrderModel>
     */
    private List<OrderModel> parseDataList(List<OrderDTO> orderDTOList){
        List<OrderModel> OrderModelList = new ArrayList<>();
        orderDTOList.forEach(orderDTO->{
            OrderModel orderModel = TransformUtils.transform(orderDTO.getOrderBase(), OrderModel.class);
            orderModel.setOrderItemList(TransformUtils.transformList(orderDTO.getOrderItemList(), OrderItemsModel.class));
            orderModel.setOrderDelivery(TransformUtils.transform(orderDTO.getOrderDelivery(), OrderDeliveryModel.class));
            OrderModelList.add(orderModel);
        });
        return OrderModelList;
    }


}
